package com.kkshop.utility;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.PropertyResourceBundle;
import java.util.Set;

public class DefaultingPropertyResourceBundle extends PropertyResourceBundle {

    public DefaultingPropertyResourceBundle(InputStream inputStream) throws IOException {
        super(inputStream);
    }

    public Enumeration<String> getKeys() {
        Set<String> keys = new HashSet<String>();
        Enumeration<String> en = super.getKeys();
        while (en.hasMoreElements()) {
            keys.add(en.nextElement());
        }
        
        Set systemPropertyKeys = System.getProperties().keySet();
        for (Object key : systemPropertyKeys) {
            keys.add((String) key);
        }
        
        final Iterator<String> it = keys.iterator();
        return new Enumeration<String>() {
            public boolean hasMoreElements() {
                return it.hasNext();
            }
            
            public String nextElement() {
                return it.next();
            }
        };
    }

    public Object handleGetObject(String string) {
        String value = System.getProperty(string);
        if (value != null) {
            return value;
        } else {
            return super.handleGetObject(string);
        }
    }
}
